package com.jfinal.code.eclipse.plugin.wizards;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;

import com.jfinal.code.eclipse.plugin.generator.dto.ValueDTO;
import com.jfinal.code.eclipse.plugin.generator.strategy.GFactory;
import com.jfinal.code.eclipse.plugin.wizards.page.DatabaseInfoWizardPage;
import com.jfinal.code.eclipse.plugin.wizards.page.MakeCodeWizardPage;
import com.jfinal.code.eclipse.plugin.wizards.page.TableSelectWizardPage;

/**
 * This is a sample new wizard. Its role is to create a new file resource in the
 * provided container. If the container resource (a folder or a project) is
 * selected in the workspace when the wizard is opened, it will accept it as the
 * target container. The wizard creates one file with the extension "java". If a
 * sample multi-page editor (also available as a template) is registered for the
 * same extension, it will be able to open it.
 */

public class JFinalCodeWizard extends Wizard implements INewWizard {

	private ISelection selection;
	// 数据库信息页
	private DatabaseInfoWizardPage databaseInfoWp;
	// 数据库表选择页
	private TableSelectWizardPage tableSelectWp;
	// 代码生成策略页
	private MakeCodeWizardPage makeCodeWp;

	/**
	 * Constructor for JfinalCodeWizard.
	 */
	public JFinalCodeWizard() {
		super();
		setNeedsProgressMonitor(true);
	}

	/**
	 * Adding the page to the wizard.
	 */

	public void addPages() {
		databaseInfoWp = new DatabaseInfoWizardPage(selection);
		tableSelectWp = new TableSelectWizardPage(selection);
		makeCodeWp = new MakeCodeWizardPage(selection);
		addPage(databaseInfoWp);
		addPage(tableSelectWp);
		addPage(makeCodeWp);
	}

	/**
	 * This method is called when 'Finish' button is pressed in the wizard. We
	 * will create an operation and run it using wizard as execution context.
	 */
	public boolean performFinish() {
		final String[] tableNames = tableSelectWp.getListItems();
		final String[] selectedTableNames = tableSelectWp.getSelectedList().getItems();
		final int strategy = makeCodeWp.getTabFolder().getSelectionIndex();
		final ValueDTO valueDTO = new ValueDTO();
		valueDTO.setDatabaseInfoValueDTO(databaseInfoWp.getValue());
		valueDTO.setJFinalValueDTO(makeCodeWp.getJFinalValue());
		valueDTO.setCustomValueDTO(makeCodeWp.getCustomValue());
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
				try {
					doFinish(tableNames, selectedTableNames, strategy, valueDTO, monitor);
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}
		};
		try {
			getContainer().run(true, false, op);
		} catch (InterruptedException e) {
			return false;
		} catch (InvocationTargetException e) {
			Throwable realException = e.getTargetException();
			MessageDialog.openError(getShell(), "Error", realException.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * The worker method. It will find the container, create the file if missing
	 * or just replace its contents, and open the editor on the newly created
	 * file.
	 */

	private void doFinish(String[] tableNames, String[] selectedTableNames, int strategy, ValueDTO valueDTO, IProgressMonitor monitor)
			throws CoreException {
		monitor.beginTask("Generating Code...", 2);
		// 开始生成代码
		GFactory.getStrategy(strategy).generateCode(tableNames, selectedTableNames, valueDTO);
		monitor.worked(1);
	}
	/**
	 * We will accept the selection in the workbench to see if we can initialize
	 * from it.
	 * 
	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}
}